package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.log.CommonOperateLogContentVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.log.CommonOperateLogForAddVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.log.FlowCommonOperateLogVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowCommonOperateLogRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowCommonOperateLog;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowCommonOperateLogExample;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowCommonOperateLogDAO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lilh
 * @date 2019-08-19 15:51
 */
@Service
public class FlowCommonOperateLogRepositoryImpl implements FlowCommonOperateLogRepository {

    @Autowired
    private FlowCommonOperateLogDAO flowCommonOperateLogDAO;

    @Override
    public Long insertOperateLog(FlowCommonOperateLogVo vo) {
        vo.setCreateTime(new Date());
        vo.setUpdateTime(vo.getCreateTime());
        FlowCommonOperateLog entity = convert(vo);
        flowCommonOperateLogDAO.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public Long insertOperateLog(CommonOperateLogForAddVo commonOperateLogForAddVo, CommonOperateLogContentVo contentVo) {
        FlowCommonOperateLogVo entity = new FlowCommonOperateLogVo();
        entity.setModelKey(commonOperateLogForAddVo.getModelKey());
        entity.setProcessInstanceId(commonOperateLogForAddVo.getProcessInstanceId());
        entity.setTaskId(commonOperateLogForAddVo.getTaskId());
        entity.setTaskName(commonOperateLogForAddVo.getTaskName());
        entity.setActivityId(commonOperateLogForAddVo.getActivityId());
        entity.setContent(JSONObject.toJSONString(contentVo));
        entity.setType(commonOperateLogForAddVo.getType());
        return insertOperateLog(entity);
    }

    @Override
    public List<FlowCommonOperateLogVo> listOperateLogs(String taskId) {
        FlowCommonOperateLogExample query = new FlowCommonOperateLogExample();
        query.createCriteria().andTaskIdEqualTo(taskId);
        query.setOrderByClause(" create_time desc");
        return listOperateLogs(query);
    }

    public List<FlowCommonOperateLogVo> listOperateLogs(FlowCommonOperateLogExample query) {
        return flowCommonOperateLogDAO.selectByExampleWithBLOBs(query).stream().map(this::convert).collect(Collectors.toList());
    }

    private FlowCommonOperateLog convert(FlowCommonOperateLogVo entity) {
        if (Objects.isNull(entity)) {
            return null;
        }

        FlowCommonOperateLog result = new FlowCommonOperateLog();
        BeanUtils.copyProperties(entity, result);
        return result;
    }

    private FlowCommonOperateLogVo convert(FlowCommonOperateLog entity) {
        if (Objects.isNull(entity)) {
            return null;
        }

        FlowCommonOperateLogVo result = new FlowCommonOperateLogVo();
        BeanUtils.copyProperties(entity, result);
        return result;
    }
}
